<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="kindeditor-4.1.10/jquery-1.10.2.min.js"></script>
</head>
<body>
昵称：<input id="name">
对方昵称：<input id="other">
内容:<textarea id="content"></textarea>
<input type="button" onclick="reg()" value="注册">
<input type="button" onclick="sendMessage()" value="send">
<script type="text/javascript">
    var ws = null;

    function reg() {
        if (ws == null) {
            ws = new WebSocket("ws://localhost:8080/chat/" + $("#name").val());
            ws.onopen = function () {
                // Web Socket 已连接上，使用 send() 方法发送数据
                // var message = {"other": $("#other").val(), "content": $("#content").val()}
                //ws.send(JSON.stringify(message));
            };
            ws.onmessage = function (evt) {//接收服务端传回来的消息
                var message = evt.data;
                alert("服务端说的是：" + message);
            };
        }

    }

    function sendMessage() {
        var message = {"other": $("#other").val(), "content": $("#content").val()}
        if (ws != null)
            ws.send(JSON.stringify(message));
    }
</script>
</body>
</html>